import React, {useEffect, useState} from "react";
import {Button, message, Table, Tag} from "antd";
import {Link, useNavigate} from "react-router-dom";
import axios from "axios";

const auditStateList= ["草稿箱", "审核中","已通过","未通过"];
const colorList= ['', 'orange', 'green', 'red'];
export default function AuditList() {
    const [dataSource, setDataSource] = useState([]);
    const navigate= useNavigate();
    const columns = [
        {
            title: 'ID',
            dataIndex: 'id',
            render: (id)=><b>{id}</b>
        },
        {
            title: '新闻标题',
            dataIndex: 'title',
            render: (title, record)=>{
                // return <a href={"#/news-manage/preview/" + record.id}>{title}</a>
                return <Link to={"/news-manage/preview/" + record.id}>{title}</Link>
            }
        },
        {
            title: '作者',
            dataIndex: 'author'
        },
        {
            title: '新闻分类',
            dataIndex: 'category',
            render: (category)=>category.title
        },
        {
            title: '审核状态',
            dataIndex: 'auditState',
            render: (auditState)=><Tag color={colorList[auditState]}>{auditStateList[auditState]}</Tag>
        },
        {
            title: '操作',
            key: 'operation',
            render: (record) => {
                if(record.auditState===1) {
                    return <Button type="dashed" onClick={()=>handleRevocation(record)}>撤销</Button>
                } else if(record.auditState===2) {
                    return <Button danger onClick={()=>handlePublish(record)}>发布</Button>
                } else if(record.auditState===3) {
                    return <Button type="primary" onClick={()=>navigate('/news-manage/update/'+record.id)}>修改</Button>
                }
            },
        }
    ];

    const { username } = JSON.parse(localStorage.getItem('token'));
    useEffect(()=>{
        // auditState_ne=0表示审核状态不等于0  publishState_lte=1表示发布状态小于等于1
        axios.get(`/api/news?author=${username}&auditState_ne=0&publishState=0&_expand=category`).then(response=>{
            // console.log(response.data);
            const data= response.data;
            setDataSource(data);
        });
    }, [username]);

    // 撤销
    const handleRevocation= (record)=>{
        setDataSource(dataSource.filter(item=>item.id!==record.id));
        axios.patch(`/api/news/${record.id}`, {
            auditState: 0  // 草稿箱
        }).then(res=>{
            message.info(`您可以到草稿箱中查看${record.title}这篇新闻`);
        })
    }
    // 发布
    const handlePublish= (record)=>{
        axios.patch(`/api/news/${record.id}`, {
            publishState: 1, // 已发布
            publishTime: Date.now()
        }).then(res=>{
            navigate('/publish-manage/published');
            message.success(`${record.title}这篇新闻已发布成功，您可以到【发布管理/已发布】中查看`);
        })
    }

    return (
        <div>
            <Table dataSource={dataSource} columns={columns} pagination={{ pageSize: 5 }} rowKey={ item=>item.id }/>
        </div>
    );
}
